package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// 关联表
type MaObjAssociation struct {
	ID              string `gorm:"primaryKey;column:id;autoIncrement;type:bigint unsigned;not null;comment:唯一ID" json:"id" json_old:"ID"`
	AssociationType int    `json:"association_type" gorm:"column:association_type;type:int;size:32;index:idx_type_obj_association,unique,priority:1;index:idx_type_association_obj,priority:1;comment:关联类型"`
	ObjID           string `json:"obj_id" gorm:"column:obj_id;type:string;size:30;index:idx_type_obj_association,unique,priority:2;index:idx_type_association_obj,priority:3;comment:对象ID"`
	AssociationID   string `json:"association_id" gorm:"column:association_id;type:string;size:30;index:idx_type_obj_association,unique,priority:3;index:idx_type_association_obj,priority:2;comment:关联id"`
	CreatedAt       int64  `gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间" json:"created_at"`
}

// ma_obj_association
func (m MaObjAssociation) TableName() string {
	return "ma_obj_association"
}

// BeforeCreate 自增id
func (m *MaObjAssociation) BeforeCreate(tx *gorm.DB) (err error) {
	if m.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	m.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &MaObjAssociation{})
}
